﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;
using FTchina;

public partial class UI_SendSmsSMSver : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        FTchina.basic.ChkPoewr(this, FTchina.Api.htmlpath.GetCurrentPagePath());
        if (!IsPostBack)
        {
            BindGroup();
            BindSms_Example();
        }

    }


    protected void BindSms_Example()
    {
        string tmp = string.Empty;
        string format = "  <input type='radio' name='smsactive' id='{0}'  /><span>{1}</span><br>";
        string sql = "select * from SMS_Example order by isdefault desc";
        
        using (DataTable dt = shujuku.GetDataTable(sql))
        {
            foreach (DataRow dr in dt.Rows)
            {
                tmp += string.Format(format, "rd"+dr["id"], dr["smscontent"]);
            }
        }

        litsms.Text = tmp;

    }

    /// <summary>
    /// 绑定客户级别
    /// </summary>
    protected void BindGroup()
    {
        string sql = "select * from CustomerGroup order by id asc";
        using (DataTable dt = FTchina.shujuku.GetDataTable(sql))
        {

            ddlpcg.DataSource = dt;
            ddlpcg.DataTextField = "groupname";
            ddlpcg.DataValueField = "id";
            ddlpcg.DataBind();
        }

    }
    protected void btnOk_Click(object sender, EventArgs e)
    {
        string Scontent = txbContent.Text;
        string SendTime  =basic.GetSqliteDate();

        if (Scontent.Length == 0) { JS.Alert("请输入短信内容！"); return; }


        string ChkType = Request.Form["ChkType"].ToString();//发送方式
        string CustomerGroupID = ddlpcg.SelectedValue;
        string CustomerIDs = Request.Form["pCustomerIDs"].ToString();
        string Target = FTchina.basic.SafeRequest(txbTargets.Text);

        switch (ChkType)
        {
            case "diy":
                {//手动输入了手机号码
                    bool flag = true;
                    foreach (string str in Target.Split(','))
                    {
                        if (!basic.ValidateMobile(str))
                        {
                            flag = false;
                            JS.Alert("请检查输入的手机号码");
                            break;
                        }
                    }
                    if (!flag) { return; }
                    string Result = FTchina.SMSServer.SMSSender.Send2SeverWithLog(Target, Scontent);
                    bool SendFlag = Result.Contains("成功");

                    if (SendFlag)
                    {
                        FTchina.JS.Alert(Result);
                        FTchina.JS.JavaScriptLocationHref("sendsms.aspx");
                    }
                    else
                    {
                        FTchina.JS.Alert("发送失败，请查看发送历史记录" + Result);
                    }

                    break;
                }
            case "group":
                {
                    
                    FTchina.SMSServer.SMSSender.SendSmsByGroupID(CustomerGroupID, Scontent);
                    FTchina.JS.Alert("发送成功");
                    FTchina.JS.JavaScriptLocationHref("sendsms.aspx");
                    break;
                }
            case "self":
                {
                    if (CustomerIDs.Length == 0) { JS.Alert("请先选择发送对象！"); return; }

                    FTchina.SMSServer.SMSSender.SendSmsByCustomerIDs(CustomerIDs, Scontent);
                    FTchina.JS.Alert("发送成功");
                    FTchina.JS.JavaScriptLocationHref("sendsms.aspx");
                    break;
                }
            default:
                {
                    JS.Alert("请选择一个发送方式");
                    break;
                }
        }



    }

}